﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.UI.common;
using System.Data.SqlClient;
using rock.common;

namespace rock.UI.houseinfo
{
    public partial class UserControlHouseRoom : UserControl
    {
        DalHOUSE_ROOM dal = new DalHOUSE_ROOM();

        DalHOUSE_THING dalHOUSE_THING = new DalHOUSE_THING();

        private int CURRENT_PAGE = 1;
        int XiaoQuSeqid = 0;
        int DongSeqid = 0;


        public UserControlHouseRoom()
        {
            InitializeComponent();

           // this.userControlCommonListHeader1.addClick = addClick;
          //  this.userControlCommonListHeader1.updateClick = updateClick;
          //  this.userControlCommonListHeader1.deleteClick = deleteClick;

         //  this.userControlCommonListHeader1.searchClick = searchClick;

        //    this.userControlCommonListHeader1.importClick = importClick;
     
            //this.userControlCommonListHeader1.selectClick = selectClick;

            this.Dock = System.Windows.Forms.DockStyle.Fill;

            this.userControlCommonListPage1.pageClick = refreshData;

            this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;

          //  this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
          // this.dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            ViewUtils.setDataGridViewStlye(this.dataGridView1);
            ViewUtils.setDataGridViewStlye(this.dataGridView2);

            //this.userControlCommonListPage1.toolStripLabel1.ComboBox.SelectedIndex = 0;
 
        }
        int HouseRentStatus = -1;
        private void rented_Click(object sender, EventArgs e)
        {
            HouseRentStatus = Constant.HOUSE_ROOM_STATUS_RENTED;
            this.refreshData(this.CURRENT_PAGE);
        }

        private void unrent_Click(object sender, EventArgs e)
        {
            HouseRentStatus = Constant.HOUSE_ROOM_STATUS_UNRENT;
            this.refreshData(this.CURRENT_PAGE);
        }
        private void yudiang_toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            HouseRentStatus = Constant.HOUSE_ROOM_STATUS_YUDING;
            this.refreshData(this.CURRENT_PAGE);
        }

        private void pause_toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            HouseRentStatus = Constant.HOUSE_ROOM_STATUS_PAUSE;
            this.refreshData(this.CURRENT_PAGE);
        }
        private void allhouse_Click(object sender, EventArgs e)
        {
            HouseRentStatus = -1;
            this.refreshData(this.CURRENT_PAGE);
        }

        private void detailselect_Click(object sender, EventArgs e)
        {

        }
      
        //刷新数据
        public void refreshData(int goPage)
        {
          

           CURRENT_PAGE = goPage;
           String search = this.textBox1.Text;

           StringBuilder where = new StringBuilder(
                "(" +
                        " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
               " OR " + " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
               " OR " + " HOUSE_OWNER.NAME LIKE '%" + search + "%' " +
               " OR " + " TEMP.XIAOQU_NAME LIKE '%" + search + "%' " +
               " OR " + " TEMP.DONG_NAME LIKE '%" + search + "%' " +
               ")"
               );
           if (XiaoQuSeqid >0)
           {
               where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
           if (DongSeqid > 0)
           {
               where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
           }
           


           List<int> status = new List<int>();
           if (checkBoxUnrent.Checked)
           {
               status.Add(Constant.HOUSE_ROOM_STATUS_UNRENT);
           }

           if (this.checkBoxRented.Checked)
           {
               status.Add(Constant.HOUSE_ROOM_STATUS_RENTED);
           }

           if (this.checkBoxYuding.Checked)
           {
               status.Add( Constant.HOUSE_ROOM_STATUS_YUDING);
           }

           if (this.checkBoxPause.Checked)
           {
                status.Add(Constant.HOUSE_ROOM_STATUS_PAUSE);
           }

           if (status.Count>0)
           {
               where.Append(" AND HOUSE_ROOM.STATUS in (  " + StringUtils.list2sqlinIds(status) + ")");
           }

           //this.STATUS.DataSource = Constant.HOUSE_ROOM_STATUS_LIST;

           PageDataSet ds = dal.GetListByPage(where.ToString(), " XIAOQU_NAME  ASC ,DONG_NAME ASC ,ROOM_FLOOR ASC ", CURRENT_PAGE, Manager.getInstance().pageSize);
           this.dataGridView1.DataSource = ds.DataSet.Tables[0];


          
        
           
          
           this.userControlCommonListPage1.Inits(ds.AllRecord , this.CURRENT_PAGE);
            

        }
        /// <summary>
        /// 双击更新房间信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            buttonUpdate_Click(null , null);
        }


        private void buttonAdd_Click(object sender, EventArgs e)
        {
            FormHouseRoomUpdate fcv = new FormHouseRoomUpdate();
            fcv.oneRefreshParentData = refreshData;
            fcv.ShowDialog(this);
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {



                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormHouseRoomUpdate fcv = new FormHouseRoomUpdate();



                fcv._ModelHOUSE_ROOM = dal.GetModel(Convert.ToInt32(seqid));
                //注册代理
                fcv.oneRefreshParentData = refreshData;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新记录.", "提示");
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {

                    //有生效合同的房屋不给删除
                    String sql = "SELECT * FROM RENT_CONTRACT WHERE HOUSE_ROOM_SEQID = @HOUSE_ROOM_SEQID AND STATUS =  " + Constant.RENT_CONTRACT_STATUS_SHENGXIAO;
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append(sql);
                    SqlParameter[] parameters = {
					        new SqlParameter("@HOUSE_ROOM_SEQID", SqlDbType.Char,10)			
                    };
                    parameters[0].Value = seqidInt;
                    if (DbHelperSQL.Exists(strSql.ToString(), parameters))
                    {
                        MessageBox.Show("此房屋有合同正在执行.不能删除!", "提示");
                        return;
                    }





                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        dal.Delete(seqidInt);
                        //刷新数据
                        refreshData(CURRENT_PAGE);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {

        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            HouseRentStatus = -1;
            refreshData(CURRENT_PAGE);
        }


        

        
       
       

         
        private void UserControlHouseRoom_Load(object sender, EventArgs e)
        {
            refreshData(1);
        }
        

        private void importClick()
        { 
        
        }
       
        private void selectClick()
        {
         //   Point vPoint = ((Control)this.userControlCommonListHeader1.button3Select).PointToScreen(new Point(0, 0));
         //   vPoint.Offset(0, ((Control)this.userControlCommonListHeader1.button3Select).Height);
         //   contextMenuStrip1.Show(vPoint); 
        }

        

        

        private void ROOM_THING_DELETE_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dataGridView2.CurrentRow!=null)
            {
                String seqid = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {
                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        dalHOUSE_THING.Delete(seqidInt);
                        //刷新数据
                        refreshDataRoomThing();
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择要记录.", "提示");
            }

        }
        
        private void ROOM_THING_UPDATE_ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             

            if (dataGridView2.CurrentRow!=null)
            {


                if (this.dataGridView1.SelectedCells.Count == 0)
                {
                    MessageBox.Show("请先选择要管理的房间.", "提示");
                    return;
                }
                String seqid = dataGridView2.Rows[dataGridView2.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                String room_seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                 

                // this.textBox1.Text = value;
                FormRoomThingUpdate fcv = new FormRoomThingUpdate();
                fcv._ModelHOUSE_THING = this.dalHOUSE_THING.GetModel(Convert.ToInt32(seqid));
                fcv._ModelHOUSE_THING.ROOM_SEQID = Convert.ToInt32(room_seqid);
                fcv.groupBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["HOUSE_NUMBER"].Value.ToString();
                
                //注册代理
                fcv.oneRefreshParentData = refreshDataRoomThing;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新记录.", "提示");
            }
        }

        private void ROOM_THING_ADD_ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (this.dataGridView1.SelectedCells.Count == 0)
            {
                MessageBox.Show("请先选择要管理的房间.", "提示");
                return;
            }

            FormRoomThingUpdate frtU = new FormRoomThingUpdate();

            String room_seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

            frtU._ModelHOUSE_THING = new Model.ModelHOUSE_THING();
            frtU._ModelHOUSE_THING.ROOM_SEQID = Convert.ToInt32(room_seqid);
            frtU.groupBox1.Text = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["HOUSE_NUMBER"].Value.ToString();
            frtU.oneRefreshParentData = refreshDataRoomThing;
            frtU.ShowDialog(this);
        }

        //刷新数据
        public void refreshDataRoomThing()
        {

            if (this.dataGridView1.SelectedCells.Count == 0)
            {
                return;
            }

            String room_seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
            if (room_seqid == null || room_seqid.Equals(""))
            {
               // MessageBox.Show("请先选择要管理的房间.");
                return;
            }
            DataSet ds = dalHOUSE_THING.GetList( "ROOM_SEQID  =  " + Convert.ToInt32(room_seqid)  );
            this.dataGridView2.DataSource = ds.Tables[0];
             
             
        }

        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {
            refreshDataRoomThing();
        }
        private void selectedTreeinfo(TreeInfo treeinfo)
        {
            if (treeinfo.NodeType == 0) 
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = 0; 
            }
            else if (treeinfo.NodeType == 1)
            {
                this.XiaoQuSeqid = treeinfo.Id;
                this.DongSeqid = 0;  
            }
            else if (treeinfo.NodeType == 2)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = treeinfo.Id;  
            }
            HouseRentStatus = -1;
            this.refreshData(this.CURRENT_PAGE);
        }

        #region 更换状态图片
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
           // loadImageWithDataGrid(this.dataGridView1, "STATUS");
            setStatusWithDataGrid(dataGridView1);
        }

        public void setStatusWithDataGrid(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["STATUS_INT"].Value;

                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["STATUS_STR"];
                    dgvcc.Value = Constant.getByRoomStatus(status).Name;
                }
                catch (Exception)
                {

                }
            }
        }

        public void loadImageWithDataGrid(DataGridView dgv, String colsName)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["STATUS_INT"].Value;
                    String name = "";
                    switch (status)
                    {
                        case Constant.HOUSE_ROOM_STATUS_UNRENT:
                            name = "green";
                            break;
                        case Constant.HOUSE_ROOM_STATUS_RENTED:
                            name = "red";
                            break;
                        case Constant.HOUSE_ROOM_STATUS_YUDING:
                            name = "yellow";
                            break;
                        case Constant.HOUSE_ROOM_STATUS_PAUSE:
                            name = "yellow";
                            break;
                        default:
                            break;
                    }
                    DataGridViewImageCell dgvcc = (DataGridViewImageCell)dgv.Rows[a].Cells[colsName];

                    dgvcc.Value = Image.FromFile(Application.StartupPath + "\\image\\status\\" + name + ".png");
                    dgvcc.ToolTipText = Application.StartupPath + "\\image\\status\\" + name + ".png";

                }
                catch (Exception)
                {

                }

            }
            // DataGridViewTextBoxCell dgvcc2 = (DataGridViewTextBoxCell)dgv.Rows[1].Cells["NAME"];
            // dgvcc2.Value = "TTTTTTTTTTTTTTT";

        }
        #endregion 更换状态图片

        //物品添加
        private void button1Add_Click(object sender, EventArgs e)
        {
            ROOM_THING_ADD_ToolStripMenuItem_Click(null, null);

        }
        //物品修改
        private void button2Update_Click(object sender, EventArgs e)
        {
            ROOM_THING_UPDATE_ToolStripMenuItem_Click(null, null);
        }
        //物品删除
        private void button3Detele_Click(object sender, EventArgs e)
        {
            ROOM_THING_DELETE_ToolStripMenuItem_Click(null, null);
        }

        /// <summary>
        /// 签合同
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1Contract_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {

                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();
                String status = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["STATUS_INT"].Value.ToString();
                if (Convert.ToInt32(status) != Constant.HOUSE_ROOM_STATUS_UNRENT) {
                    MessageBox.Show("选择的房间目前的状态不可以租赁.", "提示");
                    return;
                }
                FormRentContractUpdate fcv = new FormRentContractUpdate();
                fcv.oneRefreshParentData = refreshData;
                fcv._ModelRENT_CONTRACT.HOUSE_ROOM_SEQID = Convert.ToInt32(seqid);
                fcv.ShowDialog(this);
                fcv.oneRefreshParentData = refreshData;
            }
            else
            {
                MessageBox.Show("请选择要签合同的记录.", "提示");
            }
        }

        //双击更新设备信息.
        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            button2Update_Click(null, null);
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }
       
       

       

       
        
    }
}
